part of YFree;

int lastClickTime = 0;

///按钮
Widget YButton({required Widget child, double? width, double? height, Color? color, Decoration? decoration, EdgeInsetsGeometry? padding, EdgeInsetsGeometry? margin, required onTap(), bool enable = true, bool isInk = false, int? tapTime, BorderRadius? borderRadius = BorderRadius.zero}) {
  return Container(
    width: width,
    height: height,
    margin: margin,
    child: ClipRRect(
      borderRadius: borderRadius,
      child: Container(
        color: decoration == null ? color : null,
        decoration: decoration,
        child: isInk
            ? YInkButton(
                Padding(padding: padding ?? EdgeInsets.zero, child: child),
                onTap: onTap,
                enable: enable,
                tapTime: tapTime,
              )
            : YFlatButton(
                Padding(padding: padding ?? EdgeInsets.zero, child: child),
                onClick: onTap,
                enable: enable,
                tapTime: tapTime,
              ),
      ),
    ),
  );
}

@deprecated
TextButton YFlatButton(Widget? widget, {required onClick(), bool enable = true, int? tapTime}) {
  return TextButton(
    child: widget ?? YEmpty(),
    onPressed: enable ? () => _doTap(onClick, tapTime: tapTime) : () {},
    style: ButtonStyle(
      padding: MaterialStateProperty.all(EdgeInsets.zero),
      foregroundColor: MaterialStateProperty.all(Colors.black),
      backgroundColor: MaterialStateProperty.all(Colors.transparent),
      overlayColor: MaterialStateProperty.all(enable ? Colors.black.withOpacity(0.1) : Colors.transparent),
      textStyle: MaterialStateProperty.all(TextStyle(fontSize: 14)),
    ),
  );
}

Material YInkButton(Widget? widget, {color = Colors.transparent, bool enable = true, int? tapTime, required GestureTapCallback onTap}) {
  return Material(color: color, child: InkWell(child: widget ?? YEmpty(), onTap: enable ? () => _doTap(onTap, tapTime: tapTime) : () {}));
}

Widget YInkRoundButton(Widget? widget, {color = Colors.transparent, bool enable = true, int? tapTime, required GestureTapCallback onTap, double size = 70.0}) {
  return Container(width: size, height: size, child: ClipOval(child: Material(color: color, child: InkResponse(radius: size, child: widget ?? YEmpty(), onTap: enable ? () => _doTap(onTap, tapTime: tapTime) : () {}))));
}

void _doTap(onClick(), {int? tapTime}) {
  var nowTime = DateTime.now().millisecondsSinceEpoch;
  if ((nowTime - lastClickTime) > (tapTime ?? 100)) {
    onClick();
    lastClickTime = nowTime;
  }
}

///居右组件
Widget YRight({required Widget child}) => Row(children: [YExpanded(), child]);

///空组件
Widget YEmpty() => YEmptyContainer();

@deprecated
Widget YEmptyContainer() => SizedBox(width: 0, height: 0);

///占位组件
Widget YExpanded({int flex = 1}) => Expanded(child: YEmpty(), flex: flex);

///间距组件
Widget YSpacer({double width: 0, double height: 0}) => Container(width: width, height: height);

///标题组件
Expanded YTitleWidget(String? title, {Color? navBarTitleColor}) => Expanded(child: Center(child: Text(title ?? "", style: TextStyle(color: navBarTitleColor ?? YConfig.navBarTitleColor, fontSize: 18, fontWeight: FontWeight.w700))));

///导航条组件
Stack YAppBarWidget({String? title, Widget? leftWidget, onLeftClick()?, Widget? rightWidget, onRightClick()?, Widget? leftContainer, Widget? rightContainer, Widget? centerContainer}) {
  return Stack(children: <Widget>[
    Row(children: <Widget>[
      leftContainer == null ? Container(width: yNavBarHeight(), height: yNavBarHeight(), child: YFlatButton(leftWidget, onClick: () => onLeftClick!())) : leftContainer,
      Expanded(child: Container()),
      rightContainer == null ? Container(width: yNavBarHeight(), height: yNavBarHeight(), child: YFlatButton(rightWidget, onClick: () => onRightClick!())) : rightContainer,
    ]),
    Row(children: <Widget>[centerContainer == null ? YTitleWidget(title) : Expanded(child: Center(child: centerContainer))]),
  ]);
}

///加载图像
Widget YImage(double? width, double? height, String url, {BoxFit fit = BoxFit.contain, String? imagePlaceHolder}) {
  if (yIsEmpty(url)) return Image.asset(yIsEmpty(imagePlaceHolder) ? YConfig.imagePlaceHolder : imagePlaceHolder!, width: width, height: height, fit: fit);
  return ExtendedImage.network(
    url,
    loadStateChanged: (ExtendedImageState state) {
      switch (state.extendedImageLoadState) {
        case LoadState.loading:
          return Image.asset(yIsEmpty(imagePlaceHolder) ? YConfig.imagePlaceHolder : imagePlaceHolder!, width: width, height: height, fit: fit);
        case LoadState.completed:
          return state.completedWidget;
        case LoadState.failed:
          return yIsEmpty(imagePlaceHolder) ? Container(child: Icon(Icons.error), width: width, height: height) : Image.asset(imagePlaceHolder!, width: width, height: height, fit: fit);
      }
    },
    width: width,
    height: height,
    fit: fit,
  );
}

///加载图像(圆角)
Widget YImageCorner(double? width, double? height, String url, double corner, {BoxFit fit = BoxFit.cover, String? imagePlaceHolder}) {
  return ClipRRect(child: YImage(width, height, url, fit: fit, imagePlaceHolder: imagePlaceHolder), borderRadius: BorderRadius.all(Radius.circular(corner)));
}

///加载图像(圆形)
Widget YImageCircle(double? width, double? height, String url, {BoxFit fit = BoxFit.cover, String? imagePlaceHolder}) {
  return ClipOval(child: YImage(width, height, url, fit: fit, imagePlaceHolder: imagePlaceHolder));
}

// final kTransparentImage = Uint8List.fromList([0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x06, 0x00, 0x00, 0x00, 0x1F, 0x15, 0xC4, 0x89, 0x00, 0x00, 0x00, 0x0A, 0x49, 0x44, 0x41, 0x54, 0x78, 0x9C, 0x63, 0x00, 0x01, 0x00, 0x00, 0x05, 0x00, 0x01, 0x0D, 0x0A, 0x2D, 0xB4, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4E, 0x44, 0xAE]);

Widget YImage2(double? width, double? height, String url, {BoxFit fit = BoxFit.contain, String? imagePlaceHolder}) {
  if (yIsEmpty(url)) return Image.asset(yIsEmpty(imagePlaceHolder) ? YConfig.imagePlaceHolder : imagePlaceHolder!, width: width, height: height, fit: fit);
  return FadeInImage.assetNetwork(placeholder: yIsEmpty(imagePlaceHolder) ? YConfig.imagePlaceHolder : imagePlaceHolder!, image: url, width: width, height: height, fit: fit);
//  return FadeInImage.memoryNetwork(placeholder: kTransparentImage, image: url, width: width, height: height, fit: fit);
}

Widget YImageCorner2(double? width, double? height, String url, double corner, {BoxFit fit = BoxFit.cover, String? imagePlaceHolder}) {
  return ClipRRect(child: YImage2(width, height, url, fit: fit, imagePlaceHolder: imagePlaceHolder), borderRadius: BorderRadius.all(Radius.circular(corner)));
}

Widget YImageCircle2(double? width, double? height, String url, {BoxFit fit = BoxFit.cover, String? imagePlaceHolder}) {
  return ClipOval(child: YImage2(width, height, url, fit: fit, imagePlaceHolder: imagePlaceHolder));
}

///下拉刷新组件
Widget YRefreshContainer({required Widget child, onRefresh()?, ScrollController? scrollController, Header? header, Footer? footer, onLoadMore()?, bottomBouncing = true, scrollbar = true}) {
  var easyRefresh = EasyRefresh(
    onRefresh: onRefresh != null ? () => onRefresh() : null,
    onLoad: onLoadMore != null ? () => onLoadMore() : null,
    bottomBouncing: bottomBouncing,
    scrollController: scrollController,
    header: header ?? (onRefresh != null ? BezierCircleHeader(backgroundColor: YConfig.themeColor.withOpacity(0.1), color: YConfig.themeColor) : null),
    footer: footer ?? (onLoadMore != null ? BallPulseFooter(backgroundColor: YConfig.themeColor.withOpacity(0.1), color: YConfig.themeColor, enableInfiniteLoad: false) : null),
    child: Container(
      constraints: BoxConstraints(minHeight: 300),
      child: child,
    ),
  );
  return scrollbar ? Scrollbar(child: easyRefresh) : easyRefresh;
}

Widget YRefreshListView<T>({required List<T> models, onRefresh()?, required itemBuilder(BuildContext context, int index, T model), ScrollController? scrollController, Header? header, Footer? footer, onLoadMore()?, bottomBouncing = true, scrollbar = true, EdgeInsets padding = EdgeInsets.zero}) {
  var easyRefresh = EasyRefresh(
    onRefresh: onRefresh != null ? () => onRefresh() : null,
    onLoad: onLoadMore != null ? () => onLoadMore() : null,
    bottomBouncing: bottomBouncing,
    scrollController: scrollController,
    header: header ?? (onRefresh != null ? BezierCircleHeader(backgroundColor: YConfig.themeColor.withOpacity(0.1), color: YConfig.themeColor) : null),
    footer: footer ?? (onLoadMore != null ? BallPulseFooter(backgroundColor: YConfig.themeColor.withOpacity(0.1), color: YConfig.themeColor, enableInfiniteLoad: false) : null),
    child: Container(
      constraints: BoxConstraints(minHeight: 300),
      child: ListView.builder(
        padding: padding,
        primary: false,
        shrinkWrap: true,
        itemCount: models.length,
        itemBuilder: (context, index) => itemBuilder(context, index, models[index]),
      ),
    ),
  );
  return scrollbar ? Scrollbar(child: easyRefresh) : easyRefresh;
}

Widget YRefreshStaticListView({required List<Widget> children, onRefresh()?, ScrollController? scrollController, Header? header, Footer? footer, onLoadMore()?, bottomBouncing = true, scrollbar = true, EdgeInsets padding = EdgeInsets.zero}) {
  var easyRefresh = EasyRefresh(
    onRefresh: onRefresh != null ? () => onRefresh() : null,
    onLoad: onLoadMore != null ? () => onLoadMore() : null,
    bottomBouncing: bottomBouncing,
    scrollController: scrollController,
    header: header ?? (onRefresh != null ? BezierCircleHeader(backgroundColor: YConfig.themeColor.withOpacity(0.1), color: YConfig.themeColor) : null),
    footer: footer ?? (onLoadMore != null ? BallPulseFooter(backgroundColor: YConfig.themeColor.withOpacity(0.1), color: YConfig.themeColor, enableInfiniteLoad: false) : null),
    child: Container(
      constraints: BoxConstraints(minHeight: 300),
      child: ListView(
        padding: padding,
        primary: false,
        shrinkWrap: true,
        children: children,
      ),
    ),
  );
  return scrollbar ? Scrollbar(child: easyRefresh) : easyRefresh;
}

Widget YRefreshGridView<T>({required List<T> models, required SliverGridDelegate gridDelegate, onRefresh()?, required itemBuilder(BuildContext context, int index, T model), ScrollController? scrollController, Header? header, Footer? footer, onLoadMore()?, bottomBouncing = true, scrollbar = true, EdgeInsets padding = EdgeInsets.zero}) {
  var easyRefresh = EasyRefresh(
    onRefresh: onRefresh != null ? () => onRefresh() : null,
    onLoad: onLoadMore != null ? () => onLoadMore() : null,
    bottomBouncing: bottomBouncing,
    scrollController: scrollController,
    header: header ?? (onRefresh != null ? BezierCircleHeader(backgroundColor: YConfig.themeColor.withOpacity(0.1), color: YConfig.themeColor) : null),
    footer: footer ?? (onLoadMore != null ? BallPulseFooter(backgroundColor: YConfig.themeColor.withOpacity(0.1), color: YConfig.themeColor, enableInfiniteLoad: false) : null),
    child: Container(
      constraints: BoxConstraints(minHeight: 300),
      child: GridView.builder(
        padding: padding,
        primary: false,
        shrinkWrap: true,
        gridDelegate: gridDelegate,
        itemCount: models.length,
        itemBuilder: (context, index) => itemBuilder(context, index, models[index]),
      ),
    ),
  );
  return scrollbar ? Scrollbar(child: easyRefresh) : easyRefresh;
}

///列表条目
Widget YListTile(String title, String? image, onClick(), {Color bgColor = Colors.transparent, double imageSize = 20, Widget? right, EdgeInsets? padding, TextStyle? titleStyle, bool enable = true}) {
  return Container(
    color: bgColor,
    child: YFlatButton(
      Container(
        padding: padding ?? EdgeInsets.all(15),
        child: Row(
          children: <Widget>[
            image != null ? Image.asset(image, width: imageSize, height: imageSize, fit: BoxFit.fill) : YEmpty(),
            Expanded(child: Container(margin: EdgeInsets.fromLTRB(image != null ? 10 : 0, 0, 0, 0), child: Text(title, style: titleStyle ?? TextStyle(fontSize: 15)))),
            right != null ? right : Offstage(offstage: true),
          ],
        ),
      ),
      onClick: onClick,
      enable: enable,
    ),
  );
}

Widget YCircularProgress({double size = 50, double strokeWidth = 4}) {
  return Center(child: Container(width: size, height: size, child: CircularProgressIndicator(strokeWidth: strokeWidth)));
}

Widget YAnimatedSwitcher({required Widget child, int duration = 300}) {
  return AnimatedSwitcher(duration: Duration(milliseconds: duration), child: child);
}

///侧滑菜单
class YDrawer extends StatefulWidget {
  double elevation;
  Widget? child;
  String? semanticLabel;
  double widthPercent;
  DrawerCallback? callback;
  bool? isShowing;

  YDrawer({Key? key, this.elevation = 16.0, this.child, this.semanticLabel, this.widthPercent = 0.8, this.callback}) : super(key: key);

  @override
  _YDrawerState createState() => _YDrawerState();
}

class _YDrawerState extends State<YDrawer> {
  @override
  void initState() {
    widget.isShowing = true;
    if (widget.callback != null) widget.callback!(true);
    super.initState();
  }

  @override
  void dispose() {
    widget.isShowing = false;
    if (widget.callback != null) widget.callback!(false);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Semantics(
      scopesRoute: true,
      namesRoute: true,
      explicitChildNodes: true,
      label: widget.semanticLabel ?? MaterialLocalizations.of(context).drawerLabel,
      child: ConstrainedBox(
        constraints: BoxConstraints.expand(width: MediaQuery.of(context).size.width * widget.widthPercent),
        child: Material(elevation: widget.elevation, child: widget.child),
      ),
    );
  }
}
